<?php
session_save_path("/tmp");
session_start();
/*
 *      guess.php
 *
 *      Copyright 2010 Nolan Baker <hendersonhasselbalch@gmail.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
?>

<?php
if ($_POST['wordsAndStatus']) {
    include("func.php");
    $con = mysql_connect("localhost", "jjenning", "");
    mysql_query("use zendo;", $con);
    
    $rule = '';
    if ($_POST['rule'] == "hasSubword") {
        $rule = 'r4';
    } else if ($_POST['rule'] == 'beginsAndEnds') {
        $rule = 'r2';
    } else if ($_POST['rule'] == 'palindrome') {
        $rule = 'r3';
    } else if ($_POST['rule'] == 'doubleLetter') {
        $rule = 'r1';
    } else if ($_POST['rule'] == 'lastLetterLastHalf') {
        $rule = 'r5';
    } else if ($_POST['rule'] == 'distinctVowels') {
        $rule = 'r6';
    } else if ($_POST['rule'] == 'multipleOf3') {
        $rule = 'r7';
    } else if ($_POST['rule'] == 'startsWithS') {
        $rule = 'r0';
    }
    $win = true;
    foreach ($_POST as $key => $value) {
        if ($key == 'rule') {
            continue;
        }
        $query = "select " . $rule . " from words where word = '" . $key . "';";
        $result = mysql_query($query, $con);
        $result_array = mysql_fetch_array($result)
        if ($result_array[$rule] != $value) {
            $win = false;
            echo json_encode(array("nothing" => "nope"));
            break;
        }
    }
    
    if ($win) {
        echo json_encode(array("game_status" => "win!"));
    }
    
} else {
    # take a stone for guessing if coming from zendo.php
    $_SESSION['stones'] = $_SESSION['stones'] - 1;

    # list some words
    
    include("func.php");
    $con = mysql_connect("localhost", "jjenning", "");
    mysql_query("use zendo;", $con);
    
    $query = "select word from words where " . $_SESSION['rule'] " = TRUE order by rand() limit 8";
    
    $result = mysql_query($query, $con);
    $word_list = null;
    
    while ($row = mysql_fetch_array($result)) {
        array_push($word_list, $row['word']);
    }
    
    $json_array = json_encode(array("words" => $word_list));
    
    echo $json_array;
}




?>
